package com.taptech.doufu.util;

import java.lang.reflect.Field;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.view.View;

import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.util.DensityUtil;

public class ScreenUtil2 {
	public static int SCREEN_PX_WIDTH = WeMediaApplication.applicationContext
			.getSharedPreferences(Constant.SCREENINFO, Context.MODE_PRIVATE)
			.getInt("width", 0);
	public static int SCREEN_PX_HEIGHT = WeMediaApplication.applicationContext
			.getSharedPreferences(Constant.SCREENINFO, Context.MODE_PRIVATE)
			.getInt("height", 0);
	public static float SCREEN_DP_WIDTH;
	public static float SCREEN_DP_HEIGHT;
	public static float DENSITY = -1;

	public static int getScreenWidthPixel(Activity activity) {
		SCREEN_PX_WIDTH = activity.getWindowManager().getDefaultDisplay()
				.getWidth();
		SCREEN_DP_WIDTH = SCREEN_PX_WIDTH / getDensity(activity);
		WeMediaApplication.applicationContext
				.getSharedPreferences(Constant.SCREENINFO, Context.MODE_PRIVATE)
				.edit().putInt("width", SCREEN_PX_WIDTH).commit();
		return SCREEN_PX_WIDTH;
	}

	public static int getScreenHeightPixel(Activity activity) {
		SCREEN_PX_HEIGHT = activity.getWindowManager().getDefaultDisplay()
				.getHeight();
		SCREEN_DP_HEIGHT = SCREEN_PX_HEIGHT / getDensity(activity);
		WeMediaApplication.applicationContext
		.getSharedPreferences(Constant.SCREENINFO, Context.MODE_PRIVATE)
		.edit().putInt("height", SCREEN_PX_HEIGHT).commit();
		return SCREEN_PX_HEIGHT;
	}

	public static float getDensity(Activity activity) {
		if (DENSITY == -1) {
			DENSITY = activity.getResources().getDisplayMetrics().density;
		}
		return DENSITY;
	}

	public static int getScreenWidthDip(Activity activity) {
		return DensityUtil.px2dip(activity, getScreenWidthPixel(activity));
	}

	public static int getScreenHeightDip(Activity activity) {
		return DensityUtil.px2dip(activity, getScreenHeightPixel(activity));
	}

	public static int getStatusBarHeightPixel(Context context) {
		Class<?> c = null;
		Object obj = null;
		Field field = null;
		int x = 0, sbar = 0;
		try {
			c = Class.forName("com.android.internal.R$dimen");
			obj = c.newInstance();
			field = c.getField("status_bar_height");
			x = Integer.parseInt(field.get(obj).toString());
			sbar = context.getResources().getDimensionPixelSize(x);
		} catch (Exception e1) {
			e1.printStackTrace();
		}

		return sbar;
	}

	/**
	 * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
	 */
	public static int dip2px(float dpValue) {
		final float scale = WeMediaApplication.applicationContext
				.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	/**
	 * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
	 */
	public static int px2dip(float pxValue) {
		final float scale = WeMediaApplication.applicationContext
				.getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f);
	}
		

	/**
	 * 获取版本号(内部识别号)
	 */
	public static int getVersionCode(Context context) {
		try {
			PackageInfo pi = context.getPackageManager().getPackageInfo(
					context.getPackageName(), 0);
			return pi.versionCode;
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
	}

	public static int[] getViewAbsCoord(View view) {
		int[] location = new int[2];
		view.getLocationOnScreen(location);// 获取在整个屏幕内的绝对坐标
		return location;

	}

	public static int[] getViewAbsWindow(View view) {
		int[] location = new int[2];
		view.getLocationInWindow(location);// 获取在父组件的坐标
		return location;

	}

}
